const app = getApp();
const api = require('../../utils/api');
const Env = require('../../config/env');
const Page = require('../../utils/pageProxy');

let timer = ''; //定时器
let countdown = 10; //定时器时间

Page({
    loginRequired: true,
	data: {
        navTitle: '注册会员', //顶部导航显示的文字
        loginBtnText: '注册会员', //登录or注册按钮的文字

        isHasPhone: false, //是否已有电话号
        inviteCode: '', //邀请人的邀请码

        agree: false, //是否同意协议
        showAgreement: false,
        showSubmit: false,
        countdown: 10,
	},

	onLoad(options) {
        this.setData({
            isHasPhone: app.globalData.userInfo.phone ? true : false,
        });
        
        //扫分享码进入
        let code = '';
        if(options.scene) {
            code = options.scene; //二维码传参
        }
        this.setData({
            inviteCode: code, //记录邀请码
            loginBtnText: this.data.isHasPhone ? '立即登录' : '注册会员',
            navTitle: this.data.isHasPhone ? '会员登录' : '注册会员',
        });
        wx.setNavigationBarTitle({
            title: this.data.isHasPhone ? '会员登录' : '注册会员'
        });
    },
    onShow() {
        
    },
    goBack() {
        app.goBack();
    },
    getUserInfo() { //刷新用户信息
        api.post('/user/info')
            .then(res => {
                app.globalData.userInfo = res;
                wx.setStorageSync('userinfo', res);
                setTimeout(() => {
                    wx.navigateBack(-1);
                }, 500);
            })
    },

    agree() { //点击协议按钮或者协议链接
        if(!this.data.agree && !this.data.showAgreement) {
            this.setData({
                showAgreement: true
            });
            this.startCountdown();
        } else if(!this.data.agree && this.data.showAgreement) {
            this.setData({
                agree: true
            });
            this.closeAgreement();
        } else if(this.data.agree) {
            this.setData({
                showAgreement: true
            });
        }
    },
    closeAgreement() {
        this.setData({
            showAgreement: false,
            showSubmit: false,
            countdown: 10,
        });
        clearInterval(timer);
        countdown = 10;
    },
    startCountdown() { //阅读协议弹窗里的倒计时
        timer = setInterval(() => {
            if(countdown == 0) {
                this.setData({
                    showSubmit: true,
                });
                clearInterval(timer);
                countdown = 10;
            } else {
                countdown--;
                this.setData({
                    countdown: countdown
                })
            }
        }, 1000);
    },
    checkAgree() { //检查是否同意协议
        wx.showToast({
            title: '请先阅读并同意注册协议！',
            icon: 'none'
        });
    },

    regPhoneNumber(e) { //获取手机授权
        let params = {
            appId: Env.appId,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
        }
        api.post('/user/register/phone', params)
            .then(() => {
                this.bindInviter();
            })
    },
    bindInviter() { //绑定邀请人
        api.post(`/user/bind/inviter?inviteCode=${this.data.inviteCode}`)
            .then(() => {
                wx.showToast({
                    title: this.data.isHasPhone ? '会员登录成功' : '注册会员成功',
                    icon: 'success'
                });
                setTimeout(() => {
                    this.getUserInfo();
                }, 500);

                setTimeout(() => {
                    wx.switchTab({
                        url: '/pages/index/index'
                    })
                }, 2000);
            })
    },
})